package com.og.ouroa.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.og.ouroa.common.annotations.EnableLoginHandle;
import com.og.ouroa.common.exception.BusinessException;
import com.og.ouroa.common.result.R;
import com.og.ouroa.common.result.ResponseEnum;
import com.og.ouroa.pojo.entity.Course;
import com.og.ouroa.pojo.entity.StudentNode;
import com.og.ouroa.pojo.entity.UserInfo;
import com.og.ouroa.pojo.vo.CourseVO;
import com.og.ouroa.service.CourseService;
import com.og.ouroa.service.FileLoadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Us
 * @since 2021-12-13
 */
@CrossOrigin
@RestController
@RequestMapping("/admin/course")
@Api(tags = "课程操作接口")
public class CourseController {


    @Autowired
    FileLoadService fileLoadService;

    @Autowired
    CourseService courseService;


    @EnableLoginHandle
    @ApiOperation("查询所有课程")
    @GetMapping("/list/{page}/{limit}")
    public R list(@ApiParam(value = "当前页码", required = true) @PathVariable Long page,
                     @ApiParam(value = "每页记录数", required = true) @PathVariable Long limit){

        Page<Course> pages = new Page<>(page, limit);

        return R.ok().message("查询成功").data("page",courseService.list(pages));
    }

    @EnableLoginHandle
    @ApiOperation("查询老师对应课程")
    @GetMapping("/listBySid/{page}/{limit}/{tid}")
    public R listBySid(@ApiParam(value = "当前页码", required = true) @PathVariable Long page,
                      @ApiParam(value = "每页记录数", required = true) @PathVariable Long limit,
                       @ApiParam(value = "用户id" ) @PathVariable Integer tid){
        Page<Course> pages = new Page<>(page, limit);
        return  R.ok().message("查询成功").data("page",courseService.listBySid(pages,tid));
    }

    @EnableLoginHandle
    @ApiOperation("按课程id查询课程")
    @GetMapping("/getBycid/{cid}")
    public R getByCid( @ApiParam(value = "课程id" ) @PathVariable Integer cid){

        return  R.ok().message("查询成功").data("page",courseService.getByCid(cid));
    }

    @EnableLoginHandle
    @ApiOperation("按课程号获取课程")
    @GetMapping("/getByC_number/{c_number}")
    public R getByCnumber( @ApiParam(value = "课程id" ) @PathVariable String c_number){

        return  R.ok().message("查询成功").data("page",courseService.getByC_number(c_number));
    }

    @EnableLoginHandle
    @ApiOperation("课程封面上传")
    @PostMapping("/upload")
    public R upload(
            @ApiParam(value = "图片文件",required = true)
            @RequestParam("file")
                    MultipartFile file,
            @ApiParam(value = "用户id", required = true)
            @RequestParam("cid")
                    Integer cid){

        String url = null;
        String Filename;
        try {
            InputStream inputStream = file.getInputStream();
            Filename = file.getOriginalFilename();
            url = fileLoadService.upload(inputStream,"courseImg" , Filename);
        } catch (IOException e) {
            e.printStackTrace();
            throw new BusinessException(ResponseEnum.UPLOAD_ERROR,e);
        }

        Course course = courseService.getById(cid);

        course.setImg(url);

        courseService.updateById(course);


        return R.ok().message("封面上传成功").data("url",url);

    }

    @EnableLoginHandle(identity = UserInfo.TEACHER)
    @ApiOperation("添加课程")
    @PostMapping("/addCourse")
    public R getByCnumber( @ApiParam(value = "课程id" ) @RequestBody CourseVO courseVO){

        courseService.addCourse(courseVO);
        return  R.ok().message("添加成功");
    }

    @EnableLoginHandle(identity = UserInfo.TEACHER)
    @ApiOperation("修改课程状态")
    @PostMapping("/updateCourse/{cid}/{status}")
    public R getByCnumber(  @ApiParam(value = "课程id" ) @PathVariable Integer cid,
     @ApiParam(value = "课程状态" ) @PathVariable Integer status){

        courseService.updataStatus(cid,status);
        return  R.ok().message("修改成功");
    }



}

